package com.nari.kmd_dataservice.util;

import com.nari.kmd_dataservice.pojo.entity.User;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.regex.Pattern;

/**
 * @ClassName: NetworkUtil
 * @Description: 常用获取客户端信息的工具
 * @Author: zhangnaijiang
 * @Date: 2022/01/25 11:33
 * @Version 1.0.0
 */

public final class NetworkUtil {

    /**
     * 获取请求主机IP地址,如果通过代理进来，则透过防火墙获取真实IP地址;
     *
     * @param request
     * @return
     * @throws IOException
     */
    public final static String getIpAddress(HttpServletRequest request) throws IOException {
        // 获取请求主机IP地址,如果通过代理进来，则透过防火墙获取真实IP地址

        String ip = request.getHeader("X-Forwarded-For");

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_CLIENT_IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }
        } else if (ip.length() > 15) {
            String[] ips = ip.split(",");
            for (int index = 0; index < ips.length; index++) {
                String strIp = (String) ips[index];
                if (!("unknown".equalsIgnoreCase(strIp))) {
                    ip = strIp;
                    break;
                }
            }
        }
        return ip;
    }

    public static boolean checkUrl(String url) {
        if(url==null){
            return false;
        }
        String[] urls = url.split(":");
        if(urls==null || urls.length!=2){
            return false;
        }
        String ip = urls[0];
        String port = urls[1];
        if(!isIp(ip)){
            return false;
        }
        if(!isNumeric(port)){
            return false;
        }
        return true;
    }
    public static boolean isNumeric(String str){
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(str).matches();
    }

    protected static boolean isIp(String IP) {//判断是否是一个IP
        boolean b = false;
        IP = deleteblank(IP);
        if(IP.matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}")){
            String s[] = IP.split("\\.");
            if(Integer.parseInt(s[0])<255) {
                if (Integer.parseInt(s[1]) < 255) {
                    if (Integer.parseInt(s[2]) < 255) {
                        if (Integer.parseInt(s[3]) < 255) {
                            b = true;
                        }
                    }
                }
            }
        }
        return b;
    }

    private static String deleteblank(String iP) {//去掉IP字符串前后所有的空格
        while(iP.startsWith(" ")){
            iP= iP.substring(1,iP.length()).trim();
        }
        while(iP.endsWith(" ")){
            iP= iP.substring(0,iP.length()-1).trim();
        }
        return iP;
    }

    public static String getUrl(User user) {
        String url = "";
        if(user.getUrl()!=null){
            if(user.getUrl().contains("http")){
                url = user.getUrl();
            }
            else{
                url = "https://"+user.getUrl();
            }
        }
        if(user.getBasePath()!=null){
            url += user.getBasePath();
        }
        return url;
    }
}